﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YJKExcelFormTool.Analyser;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Kits;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Forms
{
	public partial class ImportExcelTableForm : System.Windows.Forms.Form
	{
		public TableHead SelectTableHead
		{
			get
			{
				return this.m_HeadList[this.m_SelectHeadIndex];
			}
		}

		public ImportExcelTableForm(string fontName, double fontHeight, double fontWidthScale)
		{
			this.InitializeComponent();
			this.m_FontName = fontName;
			this.m_FontHeight = fontHeight;
			this.m_FontWidthScale = fontWidthScale;
			this.m_Previewer = new Previewer(this, this, 12, 12, 500, 540, 1u);
			base.MouseWheel += this.m_Previewer.MouseWheel;
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg == 1125)
			{
				if (m.WParam.ToInt32() == 1)
				{
					if (this.m_HeadList.Count == 0)
					{
						return;
					}
					if (this.m_HeadList[this.m_SelectHeadIndex].IsNull())
					{
						return;
					}
					TableDrawingKitForPicture tableDrawingKitForPicture = new TableDrawingKitForPicture(this.m_HeadList[this.m_SelectHeadIndex], this.m_FontHeight);
					try
					{
						tableDrawingKitForPicture.Drawing();
					}
					catch (Exception)
					{
					}
					this.m_Previewer.Curves = tableDrawingKitForPicture.Curves;
					this.m_Previewer.TextNotes = tableDrawingKitForPicture.Texts;
					this.m_Previewer.FontName = this.m_FontName;
					this.m_Previewer.FontHeight = Common.MMToFeet(this.m_FontHeight);
					return;
				}
			}
			else
			{
				base.DefWndProc(ref m);
			}
		}

		private void cmbSheets_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_SelectHeadIndex = this.cmbSheets.SelectedIndex;
			this.ChangeHeadInfos();
		}

		private void ImportExcelTableForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_ExcelTableAnalyser == null)
			{
				return;
			}
			this.m_ExcelTableAnalyser.Close();
		}

		private void ChangeHeadInfos()
		{
			if (this.m_HeadList.Count == 0)
			{
				return;
			}
			if (this.m_HeadList[this.m_SelectHeadIndex].IsNull())
			{
				TableHead value = null;
				this.m_ExcelTableAnalyser.ReadDoc(this.cmbSheets.SelectedText, this.m_SelectHeadIndex, ref value);
				this.m_HeadList[this.m_SelectHeadIndex] = value;
			}
			this.Refresh();
		}

		private void btnOpenFile_Click(object sender, EventArgs e)
		{
			this.SelectExcelFile();
		}

		private void SelectExcelFile()
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			string text = "";
			int num = text.LastIndexOf("\\");
			if (num != -1)
			{
				text = text.Substring(0, num) + "\\";
			}
			openFileDialog.InitialDirectory = text;
			openFileDialog.Filter = "Excel文件|*.xls|Excel文件|*.xlsx";
			openFileDialog.RestoreDirectory = true;
			openFileDialog.FilterIndex = 1;
			if (openFileDialog.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			string fileName = openFileDialog.FileName;
			int num2 = this.cmbFillFilePath.Items.IndexOf(fileName);
			if (-1 == num2)
			{
				this.cmbFillFilePath.Items.Add(fileName);
				this.cmbFillFilePath.SelectedIndex = this.cmbFillFilePath.Items.Count - 1;
			}
			else
			{
				this.cmbFillFilePath.SelectedIndex = num2;
			}
			this.UpdateTable(fileName);
			this.Refresh();
		}

		private bool UpdateTable(string tabelTemplateName)
		{
			this.m_ExcelTableAnalyser = new ExcelTableAnalyser();
			if (!this.m_ExcelTableAnalyser.Init(tabelTemplateName))
			{
				return false;
			}
			List<string> list = new List<string>();
			this.m_ExcelTableAnalyser.GetSheetsWithDoc(tabelTemplateName, ref list);
			this.cmbSheets.Items.Clear();
			foreach (string item in list)
			{
				this.cmbSheets.Items.Add(item);
				this.m_HeadList.Add(new TableHead());
			}
			if (this.cmbSheets.Items.Count > 0)
			{
				this.cmbSheets.SelectedIndex = 0;
			}
			return true;
		}

		private void cmbFillFilePath_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateTable(this.cmbFillFilePath.SelectedText);
			this.Refresh();
		}

		private ExcelTableAnalyser m_ExcelTableAnalyser;

		private List<TableHead> m_HeadList = new List<TableHead>();

		private int m_SelectHeadIndex;

		private string m_FontName = "";

		private double m_FontHeight;

		private double m_FontWidthScale = 1.0;

		private Previewer m_Previewer;
	}
}
